package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.entity.Product;
import com.mall.mapper.OrdersMapper;
import com.mall.mapper.OrderproductMapper;
import com.mall.mapper.ProductMapper;
import com.mall.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fire
 * @since 2020-04-16
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrdersMapper orderMapper;

    @Autowired
    private OrderproductMapper orderproductMapper;

    @Override
    public List<Product> listProduct(int pageNo, int pageSize) {
        Page<Product> page = new Page<>(pageNo, pageSize);
        productMapper.selectPage(page, null);
        return page.getRecords();
    }

    @Override
    public List<Product> findByKeyword(String keyword, int pageNo, int pageSize) {
        Page<Product> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.like("name", keyword);
        productMapper.selectPage(page, wrapper);
        List<Product> productList = page.getRecords();
        productList.forEach(System.out::println);
        return productList;
    }

    @Override
    public List<Product> listProductByType(int typeId, int pageNo, int pageSize) {
        Page<Product> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("typeId", typeId);
        productMapper.selectPage(page, wrapper);
        return page.getRecords();
    }

    @Override
    public List<Product> sortByType(int sortType, int pageNo, int pageSize) {

        String flag = null;
        Page<Product> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        System.out.println(sortType);
        // 销量排序
        if (sortType == 1) {
            flag = "sales";
        } else {
            // 新品排序
            if (sortType == 2) {
                flag = "create_time";
            } else {
                // 价格排序
                flag = "launchPrice";
            }
        }

        if (sortType == 3) {
            wrapper.orderByAsc(flag);
        } else {
            wrapper.orderByDesc(flag);
        }
        System.out.println(flag);
        productMapper.selectPage(page, wrapper);
        return page.getRecords();

    }

  

    @Override
    public int addProduct(Product product) {
        return productMapper.insert(product);
    }

    @Override
    public Product selectById(int productId) {
        return productMapper.selectById(productId);
    }
    

}
